﻿@model MAPDavy.Entities.Matricula

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Css{
    <link href="@Url.Content("~/Content/jquery.autocomplete.css")" rel="stylesheet" type="text/css" />
}

@section JavaScript{
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.tabs.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {

            $("#Student_BirtDate").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy'
            });

            $("#Student_FechaIngreso").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy'
            });

            $("#tabs").tabs();

            $("#grade").change(function () {
                LoadSections($("#grade").val());
            });

            $('#reportCard').change(function () {
                var filename = $(this).val();
                if (!/\.pdf$/.test(filename)) {
                    jAlert("Selecciona un archivo tipo PDF", "Mensaje");
                    $('#reportCard').val("")
                }
            });

            /*$('#studentPhoto').change(function () {
                var filename = $(this).val();
                if (!/\.JPG$/.test(filename)) {
                    jAlert("Selecciona un archivo en formato JPG", "Mensaje");
                    $('#studentPhoto').val("");
                }
            });*/

            $("#StudentId").autocomplete('@Url.Action("GetAlumnoByIdServiceJson", "Alumnos")', {
                minChars: 0,
                width: 310,
                matchContains: "word",
                autoFill: false,
                dataType: 'json',
                parse: function (data) {
                    var rows = new Array();

                    $.each(data, function (i, item) {
                        rows[i] = { data: item, value: item.StudentId, result: item.StudentId };
                    });

                    return rows;
                },
                formatItem: function (row, i, max) {
                    return i + "/" + max + ": \"" + row.StudentId + " - " + row.LastName + ", " + row.FirtsName;
                },
                formatMatch: function (row, i, max) {
                    return row.StudentId + " " + row.FirtsName;
                },
                formatResult: function (row) {
                    return row.FirtsName;
                }
            });

            $("#StudentId").result(function (event, row, formatted) {
                if (row) {
                    $("#Student_FirtsName").val(row.FirtsName);
                    $("#Student_LastName").val(row.LastName);
                    $("#Student_BirtDate").val(row.BirtDate);
                    $("#Student_Gender option[selected='selected']").removeAttr("selected");
                    $('#Student_Gender > option[value="' + row.Gender + '"]').attr("selected", true);
                    $("#Student_FechaIngreso").val(row.fechaIngreso);
                    $("#Student_DavyId").val(row.DavyId);

                    var foto = row.FilePhotoName;

                    if (foto != null) {
                        $("#foto").fadeOut("slow", function () {
                            $("#foto").attr("src", foto);
                        });

                        $("#foto").fadeIn("slow");
                        $("#foto").css("display","block");
                    }
                    else {
                        $("#foto").fadeOut("slow", function () {
                            $("#foto").attr("src", "@Url.Content("~/Content/images/imageCapture.png")");
                        });

                        $("#foto").fadeIn("slow");
                        $("#foto").css("display","block");                        
                    }
                }
            });

            $("#guardar").click(function(e){
            
                var anio = $('#Anio').val();
                var seccion = $('#SeccionId option:selected').val();
           
               $.post('@Url.Action("ExistTutorServiceJson", "Seccion")',
                      { anio:anio ,seccionId: seccion },
                      function (ok) {
                        if (ok) {
                            $('#frmMatricula').submit();
                        }
                        else {
                            jAlert("No se puede matricular en esta sección porque aun no tiene un tutor asignado", "Advertencia");
                        }
                     },
                     "json"
               );
            });
        });

        function LoadSections(id) {
            var dd = document.getElementById("SeccionId");
            dd.options.length = 0;
            dd.options[0] = new Option("Espere...");
            dd.selectedIndex = 0;
            dd.disabled = true;

            // Control de errores

            $("#SeccionId").ajaxError(function (event, request, settings) {
                dd.options[0] = new Option("Selecciona un Grado");
            });

            // Obtenemos los datos...

            $.getJSON(
                    '@Url.Action("../Seccion/SeccionServiceJson")', // URL a la acción
                    {gradeId: id },                  // Objeto JSON con parámetros
                    function (data) {                    // Función de retorno exitoso
                        $.each(data, function (i, item) {
                            dd.options[i] = new Option(item.Seccion, item.Id);
                        });
                        dd.disabled = false;
                    });
        }
    </script>
}

<div class="cpanel ui-widget ui-widget-content ui-corner-all">
    <table class="toolbar">
        <tr>
            <td valign="middle" >
                <img src="@Url.Content("~/Content/images/newAlumno.png")" title="Alumnos" alt="Alumnos" />
            </td>
            <td class="tdHeader"><span class="header">Matricular Alumno</span></td>
            <td class="cerrado"><center>
                <a href="#" id="guardar">
                    <img src="@Url.Content("~/Content/images/save.png")" title="Crear Año Lectivo" alt="Crear Año Lectivo" />
                    <br /><span class="label">Guardar</span>
                </a></center>
            </td>
            <td><center>
                <a href="@Url.Action("Index", "Alumnos")">
                    <img src="@Url.Content("~/Content/images/back.png")" title="alumnos" alt="alumnos" />
                    <br /><span class="label">Cancelar</span>
                </a></center>
            </td>
        </tr>
    </table>
</div>
@using (Html.BeginForm("Create", "Alumnos", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmMatricula" }))
{
    @Html.ValidationSummary(true)
    
    <div id="tabs">
	<ul>
		<li><a href="#tabs-1">Información del Alumno</a></li>
	</ul>
	<div id="tabs-1">
        @if (ViewData["message"]!="")
       {
           <h3>
            <span class="field-validation-error">
                @ViewData["message"] 
            </span>
          </h3>
       }
        @Html.Hidden("Id","0")
        @Html.Hidden("Anio",ViewData["anio"].ToString() )
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td valign="top">
                  <fieldset>
                    <legend>Alumno</legend>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.StudentId, "Student Id:")
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.StudentId)
                        @Html.ValidationMessageFor(model => model.StudentId)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.FirtsName, "Firts Name:")
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.Student.FirtsName)
                        @Html.ValidationMessageFor(model => model.Student.FirtsName)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.LastName, "Last Name:")
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.Student.LastName)
                        @Html.ValidationMessageFor(model => model.Student.LastName)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.Gender, "Gender:")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("Student.Gender")
                        @Html.ValidationMessageFor(model => model.Student.Gender)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.BirtDate, "Birt Date:")
                    </div>
                    <div class="editor-field">
                         @Html.EditorFor(model => model.Student.BirtDate)
                        @Html.ValidationMessageFor(model => model.Student.BirtDate)
                    </div>              
                </fieldset>  
                </td>
                <td valign="top">
                    <fieldset>
                        <legend>Inforamción Académica</legend>        
                        <div class="editor-label">
                        @Html.LabelFor(model => model.Student.DavyId, "Student Admin Plus Id:")
                        </div>
                        <div class="editor-field">
                            @Html.TextBoxFor(model => model.Student.DavyId)
                            @Html.ValidationMessageFor(model => model.Student.DavyId)
                        </div>
                        <hr/>
                        <div class="editor-label">
                            @Html.Label("Grade:")
                        </div>
                        <div class="editor-field">
                            @Html.DropDownList("grade")
                        </div>

                        <div class="editor-label">
                            @Html.LabelFor(model => model.SeccionId, "Class Name")
                        </div>
                        <div class="editor-field">
                            @Html.DropDownList("SeccionId") 
                            @Html.ValidationMessageFor(model => model.SeccionId)
                        </div>
                        
                        <div class="editor-label">
                            @Html.LabelFor(model => model.Student.FechaIngreso, "Fecha de Ingreso al Colegio")
                        </div>
                        <div class="editor-field">
                            @Html.EditorFor(model => model.Student.FechaIngreso)
                            @Html.ValidationMessageFor(model => model.Student.FechaIngreso)
                        </div>
                                               
                        <div class="editor-label">
                            @Html.LabelFor(model => model.FileNameReportCard, "Enviar Report Card:")
                        </div>
                        <div class="editor-field">
                            <img src="@Url.Content("~/Content/images/addReportCard.png")" title="AddReportCard" alt="AddReportCard" />
                            <input type="file" id="reportCard" name="reportCard"/>                       
                        </div>                         
                    </fieldset>
                </td>
                <td valign="top">
                    <fieldset>
                        <legend>Foto</legend>
                        <img id="foto" src="@Url.Content("~/Content/images/imageCapture.png")" alt="Alternate Text" />
                            <div class="editor-field">
                                <input type="file" id="studentPhoto" name="studentPhoto"/>
                            </div> 
                    </fieldset>   
                </td>
            </tr>
        </table>
	</div>
</div> 
    
}
